package com.website.bookshelf.controller;

import com.website.bookshelf.model.dto.AuthenticationTokenDto;
import com.website.bookshelf.model.dto.UserCreationDto;
import com.website.bookshelf.model.dto.UserCredentialsDto;
import com.website.bookshelf.service.UserService;
import jakarta.mail.MessagingException;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.core.Authentication;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequiredArgsConstructor
public class UserController {
    private final UserService userService;

    @GetMapping("/login")
    public ResponseEntity<AuthenticationTokenDto> login(Authentication authentication) {
        AuthenticationTokenDto token = userService.login(authentication);
        return new ResponseEntity<>(token, HttpStatus.OK);
    }

    @PostMapping("/create-user")
    public ResponseEntity<UserCreationDto> createUserByEmail(@RequestBody UserCredentialsDto userCredentialsDto) throws MessagingException {
        UserCreationDto user = userService.createUserByEmail(userCredentialsDto);
        return new ResponseEntity<>(user, HttpStatus.CREATED);
    }
}
